package wsi.dataguide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.rygh.core.xml.XmlBuilder;

public class XmlTableDocument {

	private final List<XmlTableNode> nodes = new ArrayList<>();
	private final Map<String, String> prefixMap = new HashMap<>();

	public XmlTableDocument() {

	}

	public void addNode(XmlTableNode n) {
		nodes.add(n);
	}

	public void addPrefix(String key, String val) {
		prefixMap.put(key, val);
	}

	@Override
	public String toString() {
		XmlBuilder sbuilder = new XmlBuilder();
		StringBuilder sb = new StringBuilder();

		for (Entry<String, String> e : prefixMap.entrySet())
			sb.append(String.format(" xmlns:%s=\"%s\"", e.getKey(),
					e.getValue()));

		sbuilder.begin(String.format("document %s", sb.toString()));

		for (XmlTableNode i : nodes)
			sbuilder.append(i.toXmlString());

		sbuilder.end("document");

		return sbuilder.getValue();
	}
}
